package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.ne.framework.data.util.ChkTools;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank_level;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_new;
import com.wisemoresystem.seal.model.t_seal_type;
import com.wisemoresystem.seal.model.t_seal_type_config;

@Service
public class SealTypeConfigService extends BaseService {

	public List<Map<String, Object>> selectSealTypeConfig() {

		String sql = "select t.*,tu.uname tname from t_seal_type_config t left "
				+ "join t_user tu on t.user_id=tu._id  "
				+ "where t.status=0 order by t.created asc";

		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}
	
	public List<Map<String, Object>> selectSealType() {

		String sql = "select t.* from t_seal_type t where t.status=0 order by t.created asc";

		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}

	// 获取所有 印章类型
	public List<t_seal_type> selectAllTypeList() {
		String hql = "from t_seal_type t where t.status=0 order by t.created asc";
		List<t_seal_type> sealTypeList = (List<t_seal_type>) ht.find(hql);

		return sealTypeList;
	}

	// 获取所有 印章配置
	public List<t_seal_type_config> selectAllTypeConfigList() {
		String hql = "from t_seal_type_config t where t.status=0 order by t.created asc";
		List<t_seal_type_config> sealTypeConfigList = (List<t_seal_type_config>) ht
				.find(hql);

		return sealTypeConfigList;
	}
	// 查询我的配置列表
	public List<t_seal_type_config> selectMyConfigList(String config_ids) {
		if (ChkTools.isNull(config_ids)) {
			return new ArrayList<t_seal_type_config>();
		}

		String[] config_ids_attr = config_ids.split(",");
		final List<String> list = new ArrayList<String>();
		for (String id : config_ids_attr) {
			list.add(id);
		}

		final String hql = "from t_seal_type_config t where t.status=0 and t._id in (:ids) order by t.created asc";
		List<t_seal_type_config> sealTypeConfigList = ht
				.execute(new HibernateCallback<List<t_seal_type_config>>() {

					@Override
					public List<t_seal_type_config> doInHibernate(Session sn)
							throws HibernateException {
						Query query = sn.createQuery(hql);
						query.setParameterList("ids", list);
						return (List<t_seal_type_config>) query.list();

					}
				});

		return sealTypeConfigList;
	}
	
	public boolean checkTypeConfig(String seal_type_config_id){
		boolean flag = false;
		String hql="from t_seal_new t where t.seal_type_config_id=? and t.status=0";
		
		List<t_seal_new> sealNewConfig =(List<t_seal_new>)ht.find(hql,seal_type_config_id);
		System.out.println(sealNewConfig.toString()+"-----"+seal_type_config_id);
		if(ChkTools.isNull(sealNewConfig)){
			flag = true;
		}
		System.out.println(flag+"type_config");
		return flag;
	}

	public boolean checkType(String seal_type_id){
		boolean flag = false;
		String hql="from t_seal_new t where t.seal_type_id=? and t.status=0";
		
		List<t_seal_new> sealNew =(List<t_seal_new>)ht.find(hql,seal_type_id);
		System.out.println(sealNew.toString()+"-----"+seal_type_id);
		if(ChkTools.isNull(sealNew)){
			flag = true;
		}
		System.out.println(flag+"type");
		return flag;
	}
	
	//根据seal_id 判断销毁的权限机构级别
	public int checkSealTpyeLevel(String seal_id){
		t_seal seal = super.findObjById(seal_id, t_seal.class);
		t_seal_new seal_new = super.findObjById(seal.getSeal_new_id(), t_seal_new.class);
		t_seal_type_config type = super.findObjById(seal_new.getSeal_type_config_id(), t_seal_type_config.class);
		String lev_name = type.getVerify_destory();
		String sql = "from t_bank_level t where t.status=0 and t.name=? ";
		t_bank_level lev = super.findObj(sql, lev_name);
		
		return lev.getSort();
	}
	public static void main(String[] args) {

	}
}
